/*
 * ***** BEGIN LICENSE BLOCK *****
 * This file is part of "myWiWall".
 * Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
 * All rights reserved.
 *
 * "myWiWall" is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 * 
 * "myWiWall" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with "myWiWall"; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ***** END LICENSE BLOCK *****
 */


var xmlDoc;
var widget;

/* method to load an XML parser and call parseComplete */
function loadXML()
{
	// XML parser code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load("config.xml");
		parseComplete();
	}
	// XML parser code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load("config.xml")
		xmlDoc.onload=parseComplete;
	}
}

/* call the ConfigParser object and create the widget object */
function parseComplete()
{
	var manifest;
	try {
		manifest = configParser.parse(xmlDoc);
	} catch (e)
	{
		notify('Parsing error: ' + e);
	}
	
	widget = new Widget(manifest['id']);
	widget.relativePath = './';
	
	for (i in manifest['preferences'])
	{
		widget.preferences[manifest['preferences'][i]['name']] = manifest['preferences'][i]['default_value'];
	}

	/* visual layout */
	$('#iframeContainer').height(manifest['height']);
	
	$('#widgetTitle').append(manifest['widgetname']);
	
	try{
		onLoad();
	} catch(e)
	{
	// alert(e)
	};
}

$(document).ready(function(){

	/* widget container */
	var w = document.createElement('div');
	w.id = 'widgetContainer';

	/* table layout */
	var layout = document.createElement('table');
	layout.className = 'wdg';
	
	var tbody = document.createElement('tbody');
	layout.appendChild(tbody);
	
	for (i=0; i<3; i++)
	{
		tbody.appendChild(document.createElement('tr'));
	}

	var cells = new Array('wdg_topleft', 'wdg_top', 'wdg_topright', 'wdg_left', 'wdg_content', 'wdg_right', 'wdg_bottomleft', 'wdg_bottom', 'wdg_bottomright');
	
	for (i=0, t=0; i < cells.length; i++)
	{
		node = document.createElement('td');
		node.className = cells[i];

		tbody.childNodes[t].appendChild(node);
		if (i%3 == 2)
			t++;
	}

	/* widget title container */
	var widgetTitleContainer = document.createElement('div');
	widgetTitleContainer.className = 'widgetTitleContainer';

	/* widget title */
	widgetTitle = document.createElement('span');
	widgetTitle.id = 'widgetTitle';
	widgetTitle.className = 'widgetTitle';
	
	widgetTitleContainer.appendChild(widgetTitle);
					
	var content = document.createElement('div');
	content.id = 'iframeContainer';
	content.className = 'iframeContainer';
	
	while (document.body.childNodes.length != 0)
	{
		content.appendChild(document.body.childNodes[0]);
	}
		
	tbody.childNodes[0].childNodes[1].appendChild(widgetTitleContainer);
	tbody.childNodes[1].childNodes[1].appendChild(content);
	
	w.appendChild(layout);

	document.body.appendChild(w);
	
	/* debug window */
	var debugWindow = document.createElement('div');
	debugWindow.id = 'debugWindow';
	
	document.body.appendChild(debugWindow);
	/* load XML */
	loadXML();
});
